package user

import (
	"context"
	"strings"

	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userrpc"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserGroupListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserGroupListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserGroupListLogic {
	return &UserGroupListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserGroupListLogic) UserGroupList(req *types.UserGroupListRequest) (resp *types.UserGroupListResponse, err error) {
	rpcResp, err := l.svcCtx.UserServiceRPC.UserGroupList(l.ctx, &userrpc.UserGroupListRequest{
		GroupName: strings.TrimSpace(req.GroupName),
	})
	if err != nil {
		logc.Error(l.ctx, "UserGroupList rpc call error: %v", err)
		return &types.UserGroupListResponse{
			Groups: make([]*types.Group, 0),
		}, nil
	}

	resp = &types.UserGroupListResponse{}
	resp.Groups = make([]*types.Group, len(rpcResp.Data))
	for i, v := range rpcResp.Data {
		resp.Groups[i] = &types.Group{
			GroupId:        v.GroupId,
			GroupName:      v.GroupName,
			RechargeAmount: v.RechargeAmount,
			RechargeTimes:  v.RechargeTimes,
			OrderAmount:    v.OrderAmount,
			UsersCount:     v.UserCount,
			Perms:          v.Perms,
			Close:          v.Close,
		}
	}
	return
}
